package com.gexiaobao.pigeon.ui.fragment.mall

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.Bundle
import cn.qqtheme.framework.picker.OptionPicker
import cn.qqtheme.framework.widget.WheelView
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.ext.initClose
import com.gexiaobao.pigeon.app.model.bean.QueryExpressResponse
import com.gexiaobao.pigeon.app.model.param.UploadExpressParam
import com.gexiaobao.pigeon.app.util.RxToast
import com.gexiaobao.pigeon.app.util.TransInformation
import com.gexiaobao.pigeon.databinding.FragmentSendGoodsOutBinding
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/7/5 9:18
 * @Describe    : 售后，退货我已寄出
 */
class FragmentSendGoodsOut : BaseFragment<MallViewModel, FragmentSendGoodsOutBinding>() {

    private var shopId = 0
    private var mPosition = 0
    private var exPressId: Int = 0
    private var orderId = 0
    private var childOrderId = 0

//    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
//        super.onViewCreated(view, savedInstanceState)
//        activity?.let { StatusBarUtil.setLightMode(it) }
//        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
//    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = "填写物流信息"
//        mDatabind.includeBar.toolbar.initClose("填写物流信息") {
//            nav().navigateUp()
//        }
        shopId = requireArguments().getInt("shopId")
        orderId = requireArguments().getInt("orderId")
        childOrderId = requireArguments().getInt("childOrderId")
        mDatabind.tvLogicInfoOrderNo.transformationMethod = TransInformation()//小写自动转大写
    }

    override fun onResume() {
        super.onResume()
        onRefresh()
    }

    private fun onRefresh() {
        showLoadingAntiShake(getString(R.string.loading))
        mViewModel.getAppShopIndex(shopId)
//        mViewModel.getAppShopIndexInfo()
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.tvSubmitLogicInfo, mDatabind.llQueryExpress, mDatabind.includeBar.ivBack) {
            when (it) {
                mDatabind.includeBar.ivBack -> {
                    nav().navigateUp()
                }

                mDatabind.tvSubmitLogicInfo -> {
                    uoLoadExpressOrderNo()
                }

                mDatabind.llQueryExpress -> {
                    showLoadingAntiShake(getString(R.string.loading))
                    mViewModel.queryExpress()
                }
            }
        }
    }

    @SuppressLint("SetTextI18n")
    override fun createObserver() {
        super.createObserver()
        /** 进店首页 */
        mViewModel.storeInfoResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            mDatabind.data = it
            val address = it.address
            if (address.isNotEmpty() && address.contains("-")) {
                val adr = address.replace("-", "").replace("\\s".toRegex(), "")
                mDatabind.tvShopAddress.text = adr + it.addressDetail
            } else {
                mDatabind.tvShopAddress.text = address + it.addressDetail
            }
        }
//        /** 进店首页 */
//        mViewModel.storeBaseInfoResult.observe(viewLifecycleOwner) {
//            dismissLoadingAntiShake()
//            mDatabind.data = it
//        }
        /** 上传物流单号 */
        mViewModel.upLoadExpressResult.observe(viewLifecycleOwner) {
            nav().navigateUp()
        }
        /** 物流公司查询 */
        mViewModel.queryExpressResult.observe(viewLifecycleOwner) {
            dismissLoadingAntiShake()
            exPressInfoList(it.listData)
        }
    }

    private fun exPressInfoList(listData: ArrayList<QueryExpressResponse.ListBean>?) {
        dismissLoading()
        val key: ArrayList<String> = arrayListOf()
        if (listData != null && listData.size != 0) {
            for ((_, item) in listData.withIndex()) {
                key.add(item.name)
            }
        }
        val picker = OptionPicker(activity, key)
        val params = picker.window.attributes
        params.width = activity?.windowManager?.defaultDisplay!!.width
        picker.setCanceledOnTouchOutside(false)
        picker.setDividerRatio(WheelView.DividerConfig.FILL)
        picker.setShadowColor(Color.parseColor("#099bfb"), 40)
        picker.selectedIndex = mPosition
        picker.setCycleDisable(true)
        picker.setTitleText("请选择物流")
        picker.setTitleTextColor(Color.BLACK)
        picker.setTitleTextSize(18)
        picker.setSubmitText("确认")
        picker.setSubmitTextSize(17)
        picker.setSubmitTextColor(resources.getColor(R.color.bg_light_bllue))//确定按钮文字颜色
        picker.setCancelText("取消")
        picker.setCancelTextSize(17)
        picker.setCancelTextColor(resources.getColor(R.color.bg_light_bllue))//取消按钮文字颜色
        picker.setTextSize(18)
        picker.setLineSpaceMultiplier(3.0F)
        picker.setOnOptionPickListener(object : OptionPicker.OnOptionPickListener() {
            override fun onOptionPicked(index: Int, item: String) {
                mDatabind.tvExpressName.text = item
                mPosition = index
                exPressId = listData?.get(index)!!.id
            }
        })
        picker.show()
    }

    private fun uoLoadExpressOrderNo() {
        val expressNo = mDatabind.tvLogicInfoOrderNo.text.toString().trim()
        if (expressNo.isEmpty()) {
            RxToast.showToast("请填写物流单号")
            return
        }
        if (exPressId == 0) {
            RxToast.showToast("请选择物流类型")
            return
        }
        val body = UploadExpressParam(
            id = childOrderId,
            orderId = orderId,
            expressNo = expressNo,
            companyCode = exPressId
        )
        showLoading(getString(R.string.loading))
        mViewModel.uploadExpress(body)
    }
}